import { computed, ref, watch, watchEffect } from 'vue'
import { defineStore } from 'pinia'
import type { Dayjs } from 'dayjs'
import { client } from '@/utils/http'
import { useQuery } from '@tanstack/vue-query'
import dayjs from 'dayjs'

export interface UserItem {
  checked: boolean
  positionId: string
  userId: string
  userName: string
  value: number | string
}

export interface UserTypeItem {
  positionId: string
  positionName: string
  list: UserItem[]
}

export interface ConsultanlItem {
  positionTypeId: string
  positionTypeName: string
  list: UserTypeItem[]
}

const useFilterStore = defineStore('filter', () => {
  const date = ref<Dayjs>()

  const ymdId = computed(() => {
    return date.value ? date.value.format('YYYYMMDD') : ''
  })

  const settingDate = ref<Dayjs>()

  const settingYmdId = computed(() => {
    return settingDate.value ? settingDate.value.format('YYYYMM') : ''
  })

  const checkDataUpdateNum = ref(0)

  const { data: checkDataUpdate } = useQuery<boolean>({
    keepPreviousData: true,
    refetchInterval: 1 * 60 * 1000, // 1分钟请求一次是否有数据更新
    // refetchInterval: 5 * 1000, // 5秒钟请求一次是否有数据更新
    queryKey: ['/saleCoreBoard/checkDataUpdate'],
    queryFn: () => client('/saleCoreBoard/checkDataUpdate'),
    enabled: true
  })

  watch(checkDataUpdate, () => {
    if (checkDataUpdate.value) {
      checkDataUpdateNum.value += 1
      console.log('checkDataUpdateNum', checkDataUpdateNum.value)
    }
  })

  // setTimeout(() => {
  //   checkDataUpdateNum.value += 1
  // }, 5 * 1000)

  // setTimeout(() => {
  //   checkDataUpdateNum.value += 1
  // }, 10 * 1000)

  const getDate = ref()

  const {
    isFetching: dateLoading,
    data: dateOption,
    refetch: reGetDate
  } = useQuery<{
    startYmd: string
    endYmd: string
  }>({
    keepPreviousData: true,
    queryKey: ['/saleCoreBoard/getDate', checkDataUpdateNum],
    queryFn: () => client('/saleCoreBoard/getDate')
  })

  watchEffect(() => {
    getDate.value = reGetDate
  })

  const getConsultantList = ref()

  const consultantListUpdateNum = ref(0)

  const {
    isFetching: settingLoading,
    data: settingDataOptions,
    refetch: reGetConsultantList
  } = useQuery<ConsultanlItem[]>({
    keepPreviousData: true,
    queryKey: ['/saleCoreBoard/getConsultantList', settingYmdId, checkDataUpdateNum],
    queryFn: () => client('/saleCoreBoard/getConsultantList', { ymdId: settingYmdId.value }),
    // select(data) {
    //   return data.map(({ consultantId, consultantName, newOrderTargQty, saleTargQty }: any) => ({
    //     value: consultantId,
    //     label: consultantName,
    //     target: String(saleTargQty),
    //     saleTargQty: String(saleTargQty),
    //     newOrderTargQty: String(newOrderTargQty)
    //   }))
    // },
    enabled: computed(() => !!settingYmdId.value)
  })

  watch(dateOption, () => {
    date.value = dayjs(dateOption.value?.endYmd)

    settingDate.value = dayjs(dateOption.value?.endYmd)
  })

  watchEffect(() => {
    getConsultantList.value = reGetConsultantList
  })

  return {
    checkDataUpdateNum,
    dateLoading,
    settingLoading,
    consultantListUpdateNum,
    date,
    ymdId,
    settingDate,
    dateOption,
    settingDataOptions,
    //
    getDate,
    getConsultantList
  }
})

export default useFilterStore
